import jwt
from datetime import datetime, timedelta
from app.config import Config

def generate_token(user_id):
    """生成 JWT 令牌"""
    payload = {
        'user_id': user_id,
        'exp': datetime.utcnow() + timedelta(hours=24)  # 令牌有效期 24 小时
    }
    return jwt.encode(payload, Config.SECRET_KEY, algorithm='HS256')

def decode_token(token):
    """解码 JWT 令牌"""
    try:
        return jwt.decode(token, Config.SECRET_KEY, algorithms=['HS256'])
    except jwt.PyJWTError:
        return None
